package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.proCarryOut.AppropriationDto;
import com.dg.cuckoo.admin.dto.proCarryOut.ProCarryOutDto;
import com.dg.cuckoo.admin.repository.AppropriationRepository;
import com.dg.cuckoo.admin.repository.BasicProjectRepository;
import com.dg.cuckoo.admin.repository.ProCarryOutRepository;
import com.dg.cuckoo.admin.service.ProBasicService;
import com.dg.cuckoo.admin.service.ProjectInfoService;
import com.dg.cuckoo.admin.vo.ProBasicVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProBasicServiceimpl implements ProBasicService {

    @Autowired
    private ProjectInfoService projectInfoServer;
    @Autowired
    private BasicProjectRepository basicProjectRepository;
    @Autowired
    private ProCarryOutRepository proCarryOutRepository;
    @Autowired
    private AppropriationRepository appropriationRepository;

    @Override
    public List<ProBasicVo> query(List<String> oneLabel, List<String> twoLabel, String unit, String year, String mainName) {
        List<String> ids = projectInfoServer.findIdsByList(oneLabel, twoLabel, unit, year, mainName);
        List<ProBasicVo> vos=new ArrayList<>();
        for(String id:ids){
            ProBasicVo vo = new ProBasicVo();
            vo.setBasicProjectDto(basicProjectRepository.findById(id).get());
            List<ProCarryOutDto> htList = proCarryOutRepository.findByProjectId(id);
            vo.setHtList(htList);
            vo.setHtxj(proCarryOutAdd(htList));
            List<AppropriationDto> zjList = appropriationRepository.findByProjectId(id);
            vo.setZjList(zjList);
            vo.setZjxj(appropriationAdd(zjList));
            vos.add(vo);
        }
        return vos;
    }

    public String proCarryOutAdd(List<ProCarryOutDto> dtos){
        if(dtos.isEmpty()){
            return "0";
        }
        BigDecimal bigDecimal = new BigDecimal("0");
        for(ProCarryOutDto a:dtos){
            bigDecimal=bigDecimal.add(new BigDecimal(a.getAmount()));
        }
        return bigDecimal.toString();
    }

    public String appropriationAdd(List<AppropriationDto> dtos){
        if(dtos.isEmpty()){
            return "0";
        }
        BigDecimal bigDecimal = new BigDecimal("0");
        for(AppropriationDto a:dtos){
            bigDecimal=bigDecimal.add(new BigDecimal(a.getAmount()));
        }
        return bigDecimal.toString();
    }
}
